/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.jiaoyou.moments.service.impl;

import me.zhengjie.modules.jiaoyou.moments.domain.Moments;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.jiaoyou.moments.service.MomentsService;
import me.zhengjie.modules.jiaoyou.moments.domain.dto.MomentsQueryCriteria;
import me.zhengjie.modules.jiaoyou.moments.mapper.MomentsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author ztd
* @date 2025-11-18
**/
@Service
@RequiredArgsConstructor
public class MomentsServiceImpl extends ServiceImpl<MomentsMapper, Moments> implements MomentsService {

    private final MomentsMapper momentsMapper;

    @Override
    public PageResult<Moments> queryAll(MomentsQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(momentsMapper.findAll(criteria, page));
    }

    @Override
    public List<Moments> queryAll(MomentsQueryCriteria criteria){
        return momentsMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Moments resources) {
        momentsMapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Moments resources) {
        Moments moments = getById(resources.getId());
        moments.copy(resources);
        momentsMapper.updateById(moments);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        momentsMapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<Moments> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Moments moments : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", moments.getUserId());
            map.put("动态内容", moments.getContent());
            map.put("图片", moments.getImages());
            map.put("喜欢数", moments.getLikesCount());
            map.put("创建时间", moments.getCreatedAt());
            map.put("更新时间", moments.getUpdatedAt());
            map.put("评论数量", moments.getCommentsCount());
            map.put("收藏数量", moments.getCollectCount());
            map.put("分享数量", moments.getShareCount());
            map.put("类型", moments.getType());
            map.put("发布地址", moments.getLocation());
            map.put("视频", moments.getVideo());
            map.put("城市", moments.getCity());
            map.put("省份", moments.getProvince());
            map.put("未读", moments.getLatitude());
            map.put("经度", moments.getLongitude());
            map.put("是否推荐", moments.getIsRecommended());
            map.put("推荐原因", moments.getRecommendReason());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}